﻿using EnvControlLib;
using InteractiveWhiteboardManagement.Base.Common;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Regions;
using PersonnelLocationModule.Views;
using SurveillancePro.Base.Common;
using SurveillancePro.Base.ViewModels;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace PersonnelLocationModule.ViewModels
{
    /// <summary>
    /// 单个区域站点点位
    /// </summary>
    public class ViewModel_RegionSite : WorkspaceViewModel
    {
        #region Private Fields

        private Dictionary<string, string> _employeeIDs;
        private DelegateCommand _cmdRegionSiteDoubleClick;

        private readonly IRegionManager _regionManager;

        #endregion

        #region 构造函数

        /// <summary>
        /// 
        /// </summary>
        /// <param name="regionManager"></param>
        /// <param name="employeeIDs">区域站点人员 ID 列表</param>
        public ViewModel_RegionSite(IRegionManager regionManager,
            Dictionary<string, string> employeeIDs)
        {
            this._regionManager = regionManager;
            this._employeeIDs = employeeIDs;
        }

        #endregion

        #region Base Class Overrides

        protected override void OnDispose()
        {
        }

        #endregion

        #region 属性

        private string _regionID;
        /// <summary>
        /// 区域编号
        /// </summary>
        public string RegionID
        {
            get { return _regionID; }
            set
            {
                if (_regionID == value) return;
                _regionID = value;
                base.OnPropertyChanged("RegionID");
            }
        }

        private string _siteID;
        /// <summary>
        /// 站点编号
        /// </summary>
        public string SiteID
        {
            get { return _siteID; }
            set
            {
                if (_siteID == value) return;
                _siteID = value;
                base.OnPropertyChanged("SiteID");
            }
        }

        private string _regionName;
        /// <summary>
        /// 区域名称
        /// </summary>
        public string RegionName
        {
            get { return _regionName; }
            set
            {
                if (_regionName == value) return;
                _regionName = value;
                base.OnPropertyChanged("RegionName");
            }
        }

        private string _siteName;
        /// <summary>
        /// 站点名称
        /// </summary>
        public string SiteName
        {
            get { return _siteName; }
            set
            {
                if (_siteName == value) return;
                _siteName = value;
                base.OnPropertyChanged("SiteName");
            }
        }

        /// <summary>
        /// 双击站点结点
        /// </summary>
        [Display(GroupName = "DelegateCommand")]
        public DelegateCommand Cmd_RegionSiteDoubleClick
        {
            get
            {
                if (_cmdRegionSiteDoubleClick == null)
                {
                    _cmdRegionSiteDoubleClick = new DelegateCommand(_regionSiteDoubleClick, _canRegionSiteDoubleClick);
                }
                return _cmdRegionSiteDoubleClick;
            }
        }

        #endregion

        #region Private Methods

        #region DelegateCommand

        private bool _canRegionSiteDoubleClick()
        {
            return true;
        }


        /// <summary>
        /// 双击站点结点
        /// </summary>
        private void _regionSiteDoubleClick()
        {
            if(this._employeeIDs == null || this._employeeIDs.Count <= 0)
            {
                AutoCloseNoticeWindow.Show("该站点无人员信息！", false);
                return;
            }

            if (this._regionManager != null)
            {
                View_CurrentSiteEmployees currentEmployees = new View_CurrentSiteEmployees();
                ViewModel_CurrentSiteEmployees vmCurrentSiteEmployee = new ViewModel_CurrentSiteEmployees(this._regionManager, 
                    this._employeeIDs, this._regionName, this._siteName);
                currentEmployees.DataContext = vmCurrentSiteEmployee;

                object view = this._regionManager.Regions[RegionNames.ContentRegion].GetView(vmCurrentSiteEmployee.HeaderTitle); // 根据视图名获取对应视图
                if (view == null) // 若找不到对应视图则添加
                {
                    this._regionManager.Regions[RegionNames.ContentRegion].Add(currentEmployees, vmCurrentSiteEmployee.HeaderTitle); // 添加新的视图到当前区域中 AddToRegion
                    view = currentEmployees;
                }
                this._regionManager.Regions[RegionNames.ContentRegion].Activate(view); // 激活视图显示当前站点人员信息界面
            }
        }

        #endregion

        #endregion
    }
}
